package com.yxq.mdt.app.modules.goodslist.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.convenientbanner.utils.ScreenUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.IntentHelper;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.RouteTable;
import com.yxq.mdt.app.modules.classification.ui.ClassificationActivity;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.modules.market.bean.MallResponseBean;
import com.yxq.mdt.app.modules.market.model.MarketModel;
import com.yxq.mdt.app.modules.market.presenter.IMainMarketContract;
import com.yxq.mdt.app.modules.market.presenter.MainMarketPresenter;
import com.yxq.mdt.app.modules.market.ui.MainMallGoodsListAdapter;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.utils.GridSpacingItemDecoration;

import net.lucode.hackware.magicindicator.MagicIndicator;

import butterknife.BindView;

/**
 * @author L
 * @time
 * @describe 商品分类
 */
@Route(path = RouteTable.ROUTER_ACTIVITY_GOODS_LIST)
public class GoodsCatListActivity extends BaseActivity<MainMarketPresenter> implements IMainMarketContract.View {

    @Autowired
    String cat_id = "";
    String title;
    MainMallGoodsListAdapter mainMallGoodsListAdapter;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_cat_list;
    }

    @Override
    public void initView() {
        mPresenter = new MainMarketPresenter(this, new MarketModel());
        mainMallGoodsListAdapter = new MainMallGoodsListAdapter(R.layout.layout_item_goods_list_layout, null);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(mainMallGoodsListAdapter);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, ScreenUtil.dip2px(mActivity, 15), true));
        mainMallGoodsListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        mainMallGoodsListAdapter.isUseEmpty(false);
        mainMallGoodsListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putString("id", mainMallGoodsListAdapter.getData().get(position).getId() + "");
            IntentHelper.arouter(RouteTable.ROUTER_ACTIVITY_GOODS_DETAIL, bundle);
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getMainMarketGoodsList(cat_id);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getMainMarketGoodsMoreList(cat_id);
        });

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        //没有分类ID 就跳转全部分类
        if (TextUtils.isEmpty(cat_id)) {
            Intent intent = new Intent(mActivity, ClassificationActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        //标题
        title = getIntent().getStringExtra("title");
        mCommonTitleBar.getCenterTextView().setText(title);
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadSuccess(MallResponseBean beanList) {

    }

    @Override
    public void onLoadMarketDataFailed() {

    }

    @Override
    public void onLoadGoodsSuccess(MallGoodsBean data) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (mPresenter.page == 1) {
            mainMallGoodsListAdapter.setNewData(data.getList());
        } else {
            if (data.getList() != null && data.getList().size() != 0) {
                mainMallGoodsListAdapter.addData(data.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }

        mainMallGoodsListAdapter.isUseEmpty(!(mainMallGoodsListAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadMarketGoodsDataFailed() {

    }

    @Override
    public void onLoadGoodsMoreSuccess(MallGoodsBean data) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (mPresenter.page == 1) {
            mainMallGoodsListAdapter.setNewData(data.getList());
        } else {
            if (data.getList() != null && data.getList().size() != 0) {
                mainMallGoodsListAdapter.addData(data.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }

        mainMallGoodsListAdapter.isUseEmpty(!(mainMallGoodsListAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadMarketGoodsMoreDataFailed() {

    }

    @Override
    public void onLoadSuccess(MyPersonalBean myPersonalBean) {

    }

    @Override
    public void onLoadPersonalDataFailed() {

    }

}
